package com.youlin.aop;

import com.alibaba.fastjson.JSON;
import com.youlin.dto.BaseResDto;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class WebControllerAop {

    Logger logger = LoggerFactory.getLogger(WebControllerAop.class);

    @Around("execution(* com.youlin.service.impl.Byg*ServiceImpl.*(..)) " +
            "&& !execution(* com.youlin.service.impl.BygAdminServiceImpl.loadUserByUsername(..))"+
            "&& !execution(* com.youlin.service.impl.Byg*ServiceImpl.clean*(..))")
    public Object doAroundService(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        try {
            Object[] args = proceedingJoinPoint.getArgs();
            logger.info("{} ===>> args ===>> {}", proceedingJoinPoint.getSignature().getName(), JSON.toJSONString(args));
            Object object = proceedingJoinPoint.proceed();
            if (object instanceof BaseResDto) {
                String jsonString = JSON.toJSONString(object);
                BaseResDto baseResDto = JSON.parseObject(jsonString, BaseResDto.class);
                if (baseResDto.getStatus() == 1000)
                    logger.info("{} ===>> return ===>> {}", proceedingJoinPoint.getSignature().getName(), jsonString);
                else logger.error("{} ===>> return ===>> {}", proceedingJoinPoint.getSignature().getName(), jsonString);
            }
            return object;
        } catch (Throwable throwable) {
            logger.error("{} ===>> throwable ===>> {}", proceedingJoinPoint.getSignature().getName(), throwable);
            throw throwable;
        }
    }

}
